package com.kaesun.ufit.weight;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.RepeatListener;
import com.kaesun.ufit.tools.ToolbarView;

public class MomentumActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_home,

		/*R.id.button_goal_left, R.id.button_goal_right,
		R.id.button_set_left, R.id.button_set_right,
		R.id.button_min_left, R.id.button_min_right,
		R.id.button_max_left, R.id.button_max_right*/
	};

	private static final int MOMENTUM_GOAL_MIN = 0;
	private static final int MOMENTUM_GOAL_MAX = 99;
	private static final int MOMENTUM_SET_MIN = 0;
	private static final int MOMENTUM_SET_MAX = 9;
	private static final int MOMENTUM_GUIDE_START_MIN = 1;
	private static final int MOMENTUM_GUIDE_START_MAX = 19;
	private static final int MOMENTUM_GUIDE_RANGE_MIN = 1;
	private static final int MOMENTUM_GUIDE_RANGE_MAX = 19;

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private TextView mTextViewGoal = null;
	private TextView mTextViewSet = null;
	private TextView mTextViewStart = null;
	private TextView mTextViewRange = null;

	private int mMomentumGoal = 0;
	private int mMomentumSet = 0;
	private int mMomentumStart = 0;
	private int mMomentumRange = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_momentum);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		findViewById(R.id.button_goal_left).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeGoal(-1);
			}
		}));
		findViewById(R.id.button_goal_right).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeGoal(1);
			}
		}));
		findViewById(R.id.button_set_left).setOnTouchListener(new RepeatListener(1000, 250, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeSet(-1);
			}
		}));
		findViewById(R.id.button_set_right).setOnTouchListener(new RepeatListener(1000, 250, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeSet(1);
			}
		}));
		findViewById(R.id.button_min_left).setOnTouchListener(new RepeatListener(1000, 250, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeStart(-1);
			}
		}));
		findViewById(R.id.button_min_right).setOnTouchListener(new RepeatListener(1000, 250, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeStart(1);
			}
		}));
		findViewById(R.id.button_max_left).setOnTouchListener(new RepeatListener(1000, 250, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeRange(-1);
			}
		}));
		findViewById(R.id.button_max_right).setOnTouchListener(new RepeatListener(1000, 250, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeRange(1);
			}
		}));

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mTextViewGoal = (TextView)findViewById(R.id.text_momentum_goal);
		mTextViewSet = (TextView)findViewById(R.id.text_momentum_set);
		mTextViewStart = (TextView)findViewById(R.id.text_momentum_start);
		mTextViewRange = (TextView)findViewById(R.id.text_momentum_range);

		mToolbarView.chage2Button();
		mToolbarView.setTitle("Momentum");

		mMomentumGoal = ConfigData.getPrefInteger(this, ConfigData.PREF_MOMENTUM_COUNT, ConfigData.DEF_MOMENTUM_COUNT);
		mMomentumSet = ConfigData.getPrefInteger(this, ConfigData.PREF_MOMENTUM_SET, ConfigData.DEF_MOMENTUM_SET);
		mMomentumStart = ConfigData.getPrefInteger(this, ConfigData.PREF_MODEL_MOMENTUM_START, ConfigData.DEF_MODEL_MOMENTUM_START);
		mMomentumRange = ConfigData.getPrefInteger(this, ConfigData.PREF_MODEL_MOMENTUM_RANGE, ConfigData.DEF_MODEL_MOMENTUM_RANGE);
		changeGoal(0);
		changeSet(0);
		changeStart(0);
		changeRange(0);
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			update();
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_home:
			update();
			setResult(RESULT_CANCELED);
			finish();
			break;

			/*case R.id.button_goal_left:
		case R.id.button_goal_right:
			break;
		case R.id.button_set_left:
		case R.id.button_set_right:
			break;
		case R.id.button_min_left:
		case R.id.button_min_right:
			break;
		case R.id.button_max_left:
		case R.id.button_max_right:
			break;*/
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}


	private void changeGoal(int offset) {
		mMomentumGoal += offset;
		if (MOMENTUM_GOAL_MIN > mMomentumGoal) {
			mMomentumGoal = MOMENTUM_GOAL_MAX;
		} else if (MOMENTUM_GOAL_MAX < mMomentumGoal) {
			mMomentumGoal = MOMENTUM_GOAL_MIN;
		}
		mTextViewGoal.setText(String.format("%02d ea", mMomentumGoal));
	}

	private void changeSet(int offset) {
		mMomentumSet += offset;
		if (MOMENTUM_SET_MIN > mMomentumSet) {
			mMomentumSet = MOMENTUM_SET_MAX;
		} else if (MOMENTUM_SET_MAX < mMomentumSet) {
			mMomentumSet = MOMENTUM_SET_MIN;
		}
		mTextViewSet.setText(String.format("%d set", mMomentumSet));
	}

	private void changeStart(int offset) {
		mMomentumStart += offset;
		if (MOMENTUM_GUIDE_START_MIN > mMomentumStart) {
			mMomentumStart = MOMENTUM_GUIDE_START_MAX;
		} else if (MOMENTUM_GUIDE_START_MAX < mMomentumStart) {
			mMomentumStart = MOMENTUM_GUIDE_START_MIN;
		}
		mTextViewStart.setText(String.format("%d", mMomentumStart));
	}

	private void changeRange(int offset) {
		mMomentumRange += offset;
		if (MOMENTUM_GUIDE_RANGE_MIN > mMomentumRange) {
			mMomentumRange = MOMENTUM_GUIDE_RANGE_MAX;
		} else if (MOMENTUM_GUIDE_RANGE_MAX < mMomentumRange) {
			mMomentumRange = MOMENTUM_GUIDE_RANGE_MIN;
		}
		mTextViewRange.setText(String.format("%d", mMomentumRange));
	}

	private void update() {
		ConfigData.setPref(this, ConfigData.PREF_MOMENTUM_COUNT, mMomentumGoal);
		ConfigData.setPref(this, ConfigData.PREF_MOMENTUM_SET, mMomentumSet);
		ConfigData.setPref(this, ConfigData.PREF_MODEL_MOMENTUM_START, mMomentumStart);
		ConfigData.setPref(this, ConfigData.PREF_MODEL_MOMENTUM_RANGE, mMomentumRange);
	}
}
